const { postQuery } = require('../utils');


module.exports =  (router) => {
  router.get('/user', async function (ctx, next) {
    ctx.body = 'this a users response!';
  })

  router.post('/login', async function (ctx, next) {
    let { userName, pass } = ctx.request.body;
    let data = await postQuery(`select id, login_name, birthday from users where login_name = '${userName}' && pass = '${pass}'`)
    ctx.body = {
      code: data.length > 0? 0: 1,
      data: data[0],
      msg: data.length > 0? 'ok': '密码错误'
    };
  })

  router.post('/isValidUser', async function (ctx, next) {
    let userName = ctx.request.body.userName;
    let data = await postQuery(`select id from users where login_name = '${userName}'`)
    ctx.body = {
      code: data.length > 0? 0: 1,
      data: !!data[0],
      msg: data.length > 0? 'ok': '该用户不存在！'
    };
  })
}
